package crypto

import (
	"blueprint-crypto/internal/util"
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"fmt"
	"os"
)

// RSA解密
func RSA_Decrypt(msgfilepath string, pempath string, outFile string) {
	//打开要加密文件
	cipherText := util.Readfile(msgfilepath)
	//fmt.Println(cipherText)
	//fmt.Println(string(cipherText))
	//cipherbyte:=string(cipherText)
	buf := util.Readfile(pempath)
	//pem解码
	block, _ := pem.Decode(buf)
	//X509解码
	privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
	if err != nil {
		panic(err)
	}
	//对密文进行解密
	plainText, _ := rsa.DecryptPKCS1v15(rand.Reader, privateKey, []byte(string(cipherText)))
	//fmt.Println("解密后为：",string(plainText))
	PlainText, err := os.Create(outFile)
	defer PlainText.Close()
	PlainText.Write(plainText)
	fmt.Println(msgfilepath, "解密完成--", "解密后文件为：", outFile)

}
